package com.nodecollege.cloud.core.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring工具类
 *
 * @author liujp
 * @date 2018/11/13
 */
@Component
public class SpringUtil implements ApplicationContextAware
{
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        if (null == SpringUtil.applicationContext)
        {
            SpringUtil.applicationContext = applicationContext;
        }
    }

    /**
     * 获取Bean
     */
    public static <T> T getBean(Class<T> clazz)
    {
        return SpringUtil.applicationContext.getBean(clazz);
    }

    /**
     * 获取ApplicationContext
     */
    public static ApplicationContext getApplicationContext()
    {
        return SpringUtil.applicationContext;
    }
}